package com.supply.dl.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import butterknife.BindView;
import io.reactivex.Observable;

import com.supply.dl.R;
import com.supply.dl.app.ApiService;
import com.supply.dl.app.AppConfig;
import com.supply.dl.app.adapter.NormalAdapter;
import com.supply.dl.app.adapter.RecyclerViewDivider;
import com.supply.dl.app.adapter.ViewHolder;
import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.app.http.BaseObserver;
import com.supply.dl.app.http.HttpMethod;
import com.supply.dl.app.http.RxSchedulers;
import com.supply.dl.bean.SelectDictionaryBean;
import com.supply.dl.utils.DialogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 查询基础数据,对应文档v0.0.5 （八）基础数据
 * 仓库选择
 * <p>
 * Author: tian <br>
 * Time: 2018/7/2 10:18
 */
public class SelectBaseDataActivity extends BaseActivity {

    /**
     * 跳转过来携带的必要 action，用于说明用户是进行选择说明类型的数据
     */
    private String action;

    /**
     * 被选中的仓库
     */
    private SelectDictionaryBean selectBean;
    /**
     * 搜索输入框
     */
    @BindView(R.id.common_search_main_searchContentEdt)
    EditText editSearch;

    @BindView(R.id.common_search_main_searchContent)
    LinearLayout searchLyt;

    @BindView(R.id.recylerView)
    RecyclerView recyclerView;

    /**
     * 适配器
     */
    private NormalAdapter<SelectDictionaryBean> adapter;

    /**
     * 显示的数据源
     */
    private ArrayList<SelectDictionaryBean> depotBeans;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_select_depot;
    }

    @Override
    protected void initView() {
        showLeftBack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (selectBean == null) {
                    finish();
                    return;
                }

                DialogUtils.showDialog(SelectBaseDataActivity.this, "提示", "确定选择吗?", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        onSureSelect();
                    }
                }).show();

            }
        });

        depotBeans = new ArrayList<>();

        adapter = new NormalAdapter<SelectDictionaryBean>(this, depotBeans) {
            @Override
            protected void convert(ViewHolder holder, SelectDictionaryBean data) {
                holder.setText(R.id.txt_name, data.getText());
                ImageView state_img = holder.getView(R.id.img_select);

                if (data.isChecked()) {
                    state_img.setImageResource(R.mipmap.check_selected);
                }
                //
                else {
                    state_img.setImageResource(R.mipmap.check_normal);
                }
            }

            @Override
            protected int getItemLayoutId() {
                return R.layout.item_select_depot;
            }
        };

        adapter.setOnItemClickListener(new NormalAdapter.OnItemClickListener<SelectDictionaryBean>() {
            @Override
            public void onItemClick(ViewHolder viewHolder, SelectDictionaryBean data, int position) {

                for (SelectDictionaryBean depotBean : depotBeans) {
                    if (depotBean.getText().equals(data.getText())) {
                        depotBean.setChecked(!data.isChecked());
                    }
                    //
                    else {
                        depotBean.setChecked(false);
                    }

                    if (depotBean.isChecked()) {
                        selectBean = depotBean;
                    }

                }

                adapter.notifyDataSetChanged();
            }
        });

        recyclerView.addItemDecoration(new RecyclerViewDivider(this, LinearLayoutManager.VERTICAL));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

        setRightInVisible();
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        if (!getIntent().hasExtra("action")) {
            Toast.makeText(this, "没有action值的传入", Toast.LENGTH_SHORT).show();
            return;
        }

        action = getIntent().getStringExtra("action");
        switch (action) {

            case AppConfig.BaseDataAction.STO_ASN_NOTICE_ORDER_ORDER_STATUS:
                setTitleShow("订单类型");
                getDictionaryData(action);
                break;
            case AppConfig.BaseDataAction.WARE_HOUSE_TYPE_1:
                setTitleShow("出库库位");
                getHouseData(1);
                break;

            case AppConfig.BaseDataAction.WARE_HOUSE_TYPE_0:
                setTitleShow("入库库位");
                getHouseData(0);
                break;
            case AppConfig.BaseDataAction.WARE_SELECT_WAREHOUSE:
                setTitleShow("仓库选择");
                getWarehouseList();
                break;
            case AppConfig.BaseDataAction.WARE_SELECT_STOCK_POSITION:
                setTitleShow("库位选择");
                String whId = getIntent().getStringExtra("whId");
                getStockPositionList(whId);
                break;
            case AppConfig.BaseDataAction.WARE_SELECT_CUSTOM:
                setTitleShow("客户选择");
                getCustomList();
                break;
            case AppConfig.BaseDataAction.WARE_SELECT_SKU_UNIT:
                setTitleShow("规格选择");
                hideSearchView();
                String id = getIntent().getStringExtra("skuId");
                getSkuUnitList(id);
                break;
            case AppConfig.BaseDataAction.Company:
                setTitleShow("公司选择");
                getDictionaryData(action);
                break;
            case AppConfig.BaseDataAction.WARE_SELECT_WORKSHOP:
                setTitleShow("工厂选择");
                getFactroyList();
                break;
            case AppConfig.BaseDataAction.STO_INVENTORY_STOCK_TYPE:
                setTitleShow("库存类型");
                hideSearchView();
                getBaseData(AppConfig.BaseDataAction.STO_INVENTORY_STOCK_TYPE);
                break;
            case AppConfig.BaseDataAction.STO_INVENTORY_STOCK_STATUS:
                setTitleShow("库存状态");
                hideSearchView();
                getBaseData(AppConfig.BaseDataAction.STO_INVENTORY_STOCK_STATUS);
                break;
            case AppConfig.BaseDataAction.STO_TAKING_MODE:
                setTitleShow("盘点方式");
                getBaseData(AppConfig.BaseDataAction.STO_TAKING_MODE);
                break;
            case AppConfig.BaseDataAction.STO_TAKING_TYPE:
                setTitleShow("盘点类型");
                getBaseData(AppConfig.BaseDataAction.STO_TAKING_TYPE);
                break;
            case AppConfig.BaseDataAction.STO_DEPART:
                setTitleShow("责任单位");
                getdepartList();
                break;
            case AppConfig.BaseDataAction.STO_PERSON:
                setTitleShow("责任人");
                getpersonList(getIntent().getStringExtra("departId"));
                break;

        }


    }

    /**
     * 不显示搜索框
     */
    private void hideSearchView() {
        searchLyt.setVisibility(View.GONE);
    }

    /**
     * 获取基础数据
     *
     * @param action
     */
    private void getDictionaryData(String action) {
        //开始进行网络的访问
        ApiService mApiService = HttpMethod.getInstance().create(ApiService.class);
        mApiService.getDictionaryList(action).compose(RxSchedulers.<BaseInfo<List<SelectDictionaryBean>>>compose(this)).subscribe(new BaseObserver<List<SelectDictionaryBean>>() {
            @Override
            public void onSuccess(BaseInfo<List<SelectDictionaryBean>> baseInfo) {
                if (baseInfo != null) {
                    if (baseInfo.getData() != null) {
                        depotBeans.clear();
                        depotBeans.addAll(baseInfo.getData());
                        adapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                Toast.makeText(SelectBaseDataActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 仓库查询
     *
     * @param type 0表示入库，1表示出库
     */
    private void getHouseData(int type) {
        //开始进行网络的访问
        ApiService mApiService = HttpMethod.getInstance().create(ApiService.class);
        mApiService.getHouseList(type).compose(RxSchedulers.<BaseInfo<List<SelectDictionaryBean>>>compose(this)).subscribe(new BaseObserver<List<SelectDictionaryBean>>() {
            @Override
            public void onSuccess(BaseInfo<List<SelectDictionaryBean>> baseInfo) {
                if (baseInfo != null) {
                    if (baseInfo.getData() != null) {
                        depotBeans.clear();
                        depotBeans.addAll(baseInfo.getData());
                        adapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                Toast.makeText(SelectBaseDataActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && selectBean != null) {

            DialogUtils.showDialog(this, "提示", "确定选择吗?", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    onSureSelect();
                }
            }).show();

            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    /**
     * 确定选择
     */
    private void onSureSelect() {

        Intent intent = new Intent();
        intent.putExtra("selectBean", selectBean);
        this.setResult(RESULT_OK, intent);
        finish();
    }

    /**
     * 获取客户列表数据
     */
    private void getCustomList() {
        ApiService mApiService = HttpMethod.getInstance().create(ApiService.class);
        mApiService.getCustomerList().compose(RxSchedulers.<BaseInfo<List<SelectDictionaryBean>>>compose(this)).subscribe(new BaseObserver<List<SelectDictionaryBean>>() {
            @Override
            public void onSuccess(BaseInfo<List<SelectDictionaryBean>> baseInfo) {
                if (baseInfo != null) {
                    if (baseInfo.getData() != null) {
                        depotBeans.clear();
                        depotBeans.addAll(baseInfo.getData());
                        adapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                Toast.makeText(SelectBaseDataActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 获取规格
     */
    private void getSkuUnitList(String skuId) {
        ApiService mApiService = HttpMethod.getInstance().create(ApiService.class);
        mApiService.getBasUnitList(skuId).compose(RxSchedulers.<BaseInfo<List<SelectDictionaryBean>>>compose(this)).subscribe(new BaseObserver<List<SelectDictionaryBean>>() {
            @Override
            public void onSuccess(BaseInfo<List<SelectDictionaryBean>> baseInfo) {
                if (baseInfo != null) {
                    if (baseInfo.getData() != null) {
                        depotBeans.clear();
                        depotBeans.addAll(baseInfo.getData());
                        adapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                Toast.makeText(SelectBaseDataActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 获取责任单位
     */
    private void getdepartList() {
        ApiService mApiService = HttpMethod.getInstance().create(ApiService.class);
        mApiService.getDepartList().compose(RxSchedulers.<BaseInfo<List<SelectDictionaryBean>>>compose(this)).subscribe(new BaseObserver<List<SelectDictionaryBean>>() {
            @Override
            public void onSuccess(BaseInfo<List<SelectDictionaryBean>> baseInfo) {
                if (baseInfo != null) {
                    if (baseInfo.getData() != null) {
                        depotBeans.clear();
                        depotBeans.addAll(baseInfo.getData());
                        adapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                Toast.makeText(SelectBaseDataActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 获取责任人
     */
    private void getpersonList(String departId) {
        ApiService mApiService = HttpMethod.getInstance().create(ApiService.class);
        mApiService.getPersonList(departId).compose(RxSchedulers.<BaseInfo<List<SelectDictionaryBean>>>compose(this)).subscribe(new BaseObserver<List<SelectDictionaryBean>>() {
            @Override
            public void onSuccess(BaseInfo<List<SelectDictionaryBean>> baseInfo) {
                if (baseInfo != null) {
                    if (baseInfo.getData() != null) {
                        depotBeans.clear();
                        depotBeans.addAll(baseInfo.getData());
                        adapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                Toast.makeText(SelectBaseDataActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }


    /**
     * 基础数据的Observable
     *
     * @param action
     * @return
     */

    private void getBaseData(String action) {
        ApiService mApiService = HttpMethod.getInstance().create(ApiService.class);
        mApiService.getDictionaryList(action).compose(RxSchedulers.<BaseInfo<List<SelectDictionaryBean>>>compose(this)).subscribe(new BaseObserver<List<SelectDictionaryBean>>() {
            @Override
            public void onSuccess(BaseInfo<List<SelectDictionaryBean>> baseInfo) {
                if (baseInfo != null) {
                    if (baseInfo.getData() != null) {
                        depotBeans.clear();
                        depotBeans.addAll(baseInfo.getData());
                        adapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                Toast.makeText(SelectBaseDataActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }


    /**
     * 获取工厂列表数据
     */
    private void getFactroyList() {
        ApiService mApiService = HttpMethod.getInstance().create(ApiService.class);
        mApiService.getFactoryList().compose(RxSchedulers.<BaseInfo<List<SelectDictionaryBean>>>compose(this)).subscribe(new BaseObserver<List<SelectDictionaryBean>>() {
            @Override
            public void onSuccess(BaseInfo<List<SelectDictionaryBean>> baseInfo) {
                if (baseInfo != null) {
                    if (baseInfo.getData() != null) {
                        depotBeans.clear();
                        depotBeans.addAll(baseInfo.getData());
                        adapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                Toast.makeText(SelectBaseDataActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 获取车间列表数据
     */
    private void getWorkShopList() {
        ApiService mApiService = HttpMethod.getInstance().create(ApiService.class);
        mApiService.getFactoryList().compose(RxSchedulers.<BaseInfo<List<SelectDictionaryBean>>>compose(this)).subscribe(new BaseObserver<List<SelectDictionaryBean>>() {
            @Override
            public void onSuccess(BaseInfo<List<SelectDictionaryBean>> baseInfo) {
                if (baseInfo != null) {
                    if (baseInfo.getData() != null) {
                        depotBeans.clear();
                        depotBeans.addAll(baseInfo.getData());
                        adapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                Toast.makeText(SelectBaseDataActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 获取仓库列表数据
     */
    private void getWarehouseList() {
        ApiService mApiService = HttpMethod.getInstance().create(ApiService.class);
        mApiService.getHouseList(0).compose(RxSchedulers.<BaseInfo<List<SelectDictionaryBean>>>compose(this)).subscribe(new BaseObserver<List<SelectDictionaryBean>>() {
            @Override
            public void onSuccess(BaseInfo<List<SelectDictionaryBean>> baseInfo) {
                if (baseInfo != null) {
                    if (baseInfo.getData() != null) {
                        depotBeans.clear();
                        depotBeans.addAll(baseInfo.getData());
                        adapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                Toast.makeText(SelectBaseDataActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 获取库位(某个仓库下的库位) 列表数据
     */
    private void getStockPositionList(String whId) {
        ApiService mApiService = HttpMethod.getInstance().create(ApiService.class);
        mApiService.getStockPositionList(whId).compose(RxSchedulers.<BaseInfo<List<SelectDictionaryBean>>>compose(this)).subscribe(new BaseObserver<List<SelectDictionaryBean>>() {
            @Override
            public void onSuccess(BaseInfo<List<SelectDictionaryBean>> baseInfo) {
                if (baseInfo != null) {
                    if (baseInfo.getData() != null) {
                        depotBeans.clear();
                        depotBeans.addAll(baseInfo.getData());
                        adapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                Toast.makeText(SelectBaseDataActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
